package com.iqiyi.controller;

import com.iqiyi.dto.ActionResult;
import com.iqiyi.dto.VideoComment;
import com.iqiyi.mapper.CommentMapper;
import com.iqiyi.po.Comment;
import com.iqiyi.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * TODO
 *
 * @author YoungKai
 * @date 2021/2/24 11:22
 */
@RestController
@RequestMapping("/comment")
@Slf4j
public class CommentController {
    @Autowired(required = false)
    private CommentService commentService;
    @RequestMapping(value = "/findCommentByVid",method = RequestMethod.GET)
    public ActionResult findCommentByVid(int vid){
        List<Comment> commentByVid = commentService.findCommentByVid(vid);
        Collections.reverse(commentByVid);

        return new ActionResult(200,"find success",commentByVid);
    }
    @RequestMapping(value = "/selectComments",method = RequestMethod.GET)
    public ActionResult selectComments(Integer vid){
        log.info("------查找所有记录进来了!");
        int i = commentService.selectCommentTotal(vid);
        log.info("========"+i);
        return new ActionResult(200,"find success",i);
    }
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    public ActionResult addComment(String textarea,Integer vid){
        log.info(textarea+"----"+vid);
        System.out.println(vid);
        Comment comment = new Comment();
//        Date date = new Date();
        Calendar date = Calendar.getInstance();

        Date time = date.getTime();
//        System.currentTimeMillis();
        System.out.println(time);
//        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
////        String dateNow = dft.format(date);
////        log.info(dateNow);
        comment.setComment(textarea);
        comment.setDate(time);
        Comment comment1 = commentService.addComment(comment);
        log.info(comment1.toString());
//
        Integer cid = comment1.getCid();
        VideoComment videoComment = new VideoComment();
        videoComment.setCid(cid);
        videoComment.setVid(vid);
        System.out.println(videoComment.toString());
        int i = commentService.addVideoComment(videoComment);
        if (i>-1){
            return new ActionResult(200,"add success!",null);
        }
        return new ActionResult(203,"add fail!",null);
    }
}
